package com.shr25.robot.qq.controller;

import com.shr25.robot.qq.model.Vo.QqPluginVo;
import com.shr25.robot.qq.service.RobotManagerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @date 2022/6/28 20:00
 */
@Slf4j
@RequestMapping("plugin")
@RestController
public class PluginController {
    @Autowired
    private RobotManagerService robotManagerService;

    /**
     * 插件列表
     * @return
     */
    @GetMapping(value = "list")
    public Object list() {
        Map rs = new HashMap();
        List<QqPluginVo> qqPluginVos = robotManagerService.getAllPluginVos();
        rs.put("code", "200");
        rs.put("data", qqPluginVos);
        return rs;
    }

    /**
     * 升级插件
     * @param jaFileName 需要添加的jar的文件名
     * @return
     */
    @PostMapping(value = "add")
    public Object add(String jaFileName) {
        Map rs = new HashMap();
        robotManagerService.addRobotPluginJar(jaFileName);
        List<QqPluginVo> qqPluginVos = robotManagerService.getAllPluginVos();
        rs.put("code", "200");
        rs.put("data", qqPluginVos);
        return rs;
    }

    /**
     * 升级插件
     * @param pluginName 需要升级的插件名称
     * @return
     */
    @PostMapping(value = "update")
    public Object update(String pluginName) {
        Map rs = new HashMap();
        robotManagerService.updateRobotPluginJar(pluginName);
        List<QqPluginVo> qqPluginVos = robotManagerService.getAllPluginVos();
        rs.put("code", "200");
        rs.put("data", qqPluginVos);
        return rs;
    }

    /**
     * 卸载插件
     * @param pluginName 需要卸载的插件名称
     * @return
     */
    @PostMapping(value = "destroy")
    public Object destroy(String pluginName) {
        Map rs = new HashMap();
        robotManagerService.destroyRobotPluginJar(pluginName);
        List<QqPluginVo> qqPluginVos = robotManagerService.getAllPluginVos();
        rs.put("code", "200");
        rs.put("data", qqPluginVos);
        return rs;
    }
}
